package com.kejy.servlet;

import com.kejy.entity.Auction;
import com.kejy.mapper.AuctionWapper;
import com.kejy.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.sql.Timestamp;  // 用于数据库时间戳

@WebServlet(name = "AuctionServlet", value = "/AuctionServlet")
public class AuctionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String opr = request.getParameter("opr");
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            AuctionWapper mapper = sqlSession.getMapper(AuctionWapper.class);

            if ("selectAll".equals(opr)){
                // 查询所有拍卖
                List<Auction> list = mapper.selectList();
                request.setAttribute("list", list);
                request.getRequestDispatcher("auctionList.jsp").forward(request, response);
            } else if ("selectById".equals(opr)) {
                // 根据ID查询单个拍卖
                Integer auctionId = Integer.parseInt(request.getParameter("auctionId"));
                Auction auction = mapper.selectById(auctionId);
                request.setAttribute("auction", auction);
                request.getRequestDispatcher("auctionDetail.jsp").forward(request, response);
            } else if ("delete".equals(opr)) {
                // 删除拍卖
                Integer auctionId = Integer.parseInt(request.getParameter("auctionId"));
                mapper.delete(auctionId);
                sqlSession.commit();
                response.sendRedirect("AuctionServlet?opr=selectAll");
            } else if ("toAdd".equals(opr)) {
                // 跳转到添加页面
                request.getRequestDispatcher("auctionAdd.jsp").forward(request, response);
            } else if ("toUpdate".equals(opr)) {
                // 跳转到修改页面
                Integer auctionId = Integer.parseInt(request.getParameter("auctionId"));
                Auction auction = mapper.selectById(auctionId);
                request.setAttribute("auction", auction);
                request.getRequestDispatcher("auctionUpdate.jsp").forward(request, response);
            } else {
                // 默认查询所有
                List<Auction> list = mapper.selectList();
                request.setAttribute("list", list);
                request.getRequestDispatcher("auctionList.jsp").forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (sqlSession != null) {
                sqlSession.rollback();
            }
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "操作失败: " + e.getMessage());
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String opr = request.getParameter("opr");
        SqlSession sqlSession = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            AuctionWapper mapper = sqlSession.getMapper(AuctionWapper.class);

            if ("add".equals(opr)) {
                // 添加拍卖
                Auction auction = new Auction();
                auction.setAuctionName(request.getParameter("auctionName"));
                auction.setAuctionStartPrice(Double.parseDouble(request.getParameter("auctionStartPrice")));
                auction.setAuctionUpset(Double.parseDouble(request.getParameter("auctionUpset")));
                Date parsedDate = dateFormat.parse(request.getParameter("auctionStartTime"));
                auction.setAuctionStartTime(new Timestamp(parsedDate.getTime()));
                Date EndTime = dateFormat.parse(request.getParameter("auctionEndTime"));
                auction.setAuctionEndTime(new Timestamp(EndTime.getTime()));
                auction.setAuctionPic(request.getParameter("auctionPic"));
                auction.setAuctionPicType(request.getParameter("auctionPicType"));
                auction.setAuctionDesc(request.getParameter("auctionDesc"));

                mapper.insert(auction);
                sqlSession.commit();
                response.sendRedirect("AuctionServlet?opr=selectAll");
            } else if ("update".equals(opr)) {
                // 更新拍卖
                Auction auction = new Auction();
                auction.setAuctionId(Integer.parseInt(request.getParameter("auctionId")));
                auction.setAuctionName(request.getParameter("auctionName"));
                auction.setAuctionStartPrice(Double.parseDouble(request.getParameter("auctionStartPrice")));
                auction.setAuctionUpset(Double.parseDouble(request.getParameter("auctionUpset")));
                Date auctionStartTime = dateFormat.parse(request.getParameter("auctionStartTime"));
                auction.setAuctionStartTime(new Timestamp(auctionStartTime.getTime()));
                Date auctionEndTime = dateFormat.parse(request.getParameter("auctionEndTime"));
                auction.setAuctionEndTime(new Timestamp(auctionEndTime.getTime()));
                auction.setAuctionPic(request.getParameter("auctionPic"));
                auction.setAuctionPicType(request.getParameter("auctionPicType"));
                auction.setAuctionDesc(request.getParameter("auctionDesc"));

                mapper.update(auction);
                sqlSession.commit();
                response.sendRedirect("AuctionServlet?opr=selectAll");
            } else {
                doGet(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (sqlSession != null) {
                sqlSession.rollback();
            }
            request.setAttribute("errorMsg", "操作失败: " + e.getMessage());
            request.getRequestDispatcher("error.jsp").forward(request, response);
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }
}